package com.core.family.biz.yizhao.service.Impl;

import com.core.family.biz.yizhao.domain.po.YzSetting;
import com.core.family.biz.yizhao.service.YzSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * MaterInfoService实现类
 **/
@Service
public class YzSettingServiceImpl implements YzSettingService {


    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 添加文章
     *
     * @param article
     * @return
     */
    @Override
    public YzSetting create(YzSetting article) {
//        if (Objects.nonNull(article.getId())){
//            delete(article.getId());
//        }
        //id 存在会更新
        YzSetting save = mongoTemplate.save(article);
        return save;
    }

    /**
     * 删除文章
     *
     * @param
     */
    @Override
    public int delete(String id) {
        List<YzSetting> deleteList = new ArrayList<>();
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(id));
        mongoTemplate.remove(query, YzSetting.class);
        return 1;
    }


    @Override
    public YzSetting get(String id) {
        YzSetting byId = mongoTemplate.findById(id, YzSetting.class);
        return byId;
    }

    @Override
    public List<YzSetting> getList(YzSetting setting) {
//        Query query = new Query(Criteria.where("age").gt(18));
//        query.with(Sort.by(Sort.Direction.DESC, "age"));
//        query.skip(0).limit(3);
     //   return mongoTemplate.find(query, YzSetting.class);

        return mongoTemplate.findAll(YzSetting.class);
    }


}
